package com.jck.data.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class TimeTaskUtil {
	private static Map<String, Timer> AllJobs = new HashMap<String, Timer>();
	private static TimeTaskUtil timeTaskUtil = new TimeTaskUtil();
	/**
	 * 任务名称
	 */
    public static String JOB_NAME = "GZZ01";  

	private TimeTaskUtil() {

	}

	public synchronized static TimeTaskUtil getInstance() {
		if (timeTaskUtil == null) {
			timeTaskUtil = new TimeTaskUtil();
		}
		return timeTaskUtil;
	}

	public static void main(String[] args) throws Exception {
		// MyTimerTask mTask=new MyTimerTask();
		TimeTaskUtil ttt = new TimeTaskUtil();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				System.out.println("Hello !!!");

			}
		};
		Timer timer = new Timer();
//		ttt.scheduleJobTask(5000, timer, task);
		test();
		// ttt.scheduleJobTask( timer, task,1000);*/
	}

	public static void test() {
		try {
			System.out.println("testJob");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 立即执行, 根据执行时间（执行多次）进行处理任务
	 * @param timer
	 * @param task
	 * @param inteval
	 *            执行间隔>0
	 */
	public void addScheduleJobTask(String jobName,Timer timer, TimerTask task, int inteval) {
		if(findModifyJobExist(jobName, timer, task, inteval)){
			return;
		}
		try {
			AllJobs.put(jobName, timer);
			timer.scheduleAtFixedRate(task, 0, inteval);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description: 根据执行时间（执行多次）进行处理任务
	 * @param delay
	 *            延迟开始时间>0 毫秒
	 * @param timer
	 * @param task
	 * @param inteval
	 *            执行间隔>0 毫秒
	 */
	public void addScheduleJobTask(String jobName,int delay, Timer timer, TimerTask task, int inteval) {
		if(findModifyJobExist(jobName,delay, timer, task, inteval)){
			return;
		}
		try {
			timer.scheduleAtFixedRate(task, delay, inteval);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description: 根据执行时间（执行一次）进行处理任务
	 * @param delay
	 *            延迟开始时间>0 毫秒
	 * @param timer
	 * @param task
	 */
	public void addScheduleJobTask(final String jobName,int delay,Timer timer,final TimerTask task) {
		if(findModifyJobExist(jobName,delay,timer,task)){
			return;
		}
		try {
			TimerTask	taskNew=new  TimerTask() {
						@Override
						public void run() {
							task.run();
							if(AllJobs.get(jobName)!=null)
							AllJobs.get(jobName).cancel();
						}
					};
			AllJobs.put(jobName, timer);
			timer.schedule(taskNew, new Long(delay));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public boolean findModifyJobExist(String jobName,Timer timer, TimerTask task,int inteval){
		if(AllJobs.get(jobName)!=null){
			AllJobs.get(jobName).cancel();
			AllJobs.put(jobName, timer);
			timer.scheduleAtFixedRate(task, 0, inteval);
			return true;
		}
		return false;
		
	}
	public boolean findModifyJobExist(final String jobName,int delay,Timer timer,final TimerTask task){
		if(AllJobs.get(jobName)!=null){
			AllJobs.get(jobName).cancel();
			TimerTask	taskNew=new  TimerTask() {
				@Override
				public void run() {
					task.run();
					if(AllJobs.get(jobName)!=null)
					 AllJobs.get(jobName).cancel();
				}
			};
			AllJobs.put(jobName, timer);
			timer.schedule(taskNew, new Long(delay));
			return true;
		}
		return false;
		
	}
	public boolean findModifyJobExist(String jobName,int delay,Timer timer, TimerTask task,int inteval){
		if(AllJobs.get(jobName)!=null){
			AllJobs.get(jobName).cancel();
			AllJobs.put(jobName, timer);
			timer.scheduleAtFixedRate(task, delay, inteval);
			return true;
		}
		return false;
		
	}
	/**
	 * 移除当前任务
	 * @Description: 
	 * @param jobName
	 */
	public void removeScheduleJobTask(String jobName){
		if(AllJobs.get(jobName)!=null){
			AllJobs.get(jobName).cancel();
			AllJobs.remove(jobName);
		}
		
	}

}
